<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Android," />





  <link rel="alternate" href="/atom.xml" title="结网之路" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="本章主要讲的是 Android 系统的广播机制，并介绍了广播的使用，权限以及有序广播的用法。
GitHub 地址：完成第27章

在使用广播之前，首先回顾一下 PhotoGallery 在本章之前的逻辑：

打开程序后，如果开始推送服务，就每隔一段时间获取一次图片信息
如果图片有更新，就发出通知

那么本章我们想做到的有：

在开机以后自动启用服务（如果打开了开关）
在应用打开时图片有更新也不发">
<meta property="og:type" content="article">
<meta property="og:title" content="Android编程权威指南（第二版）学习笔记（二十七）—— 第27章 broadcast intent">
<meta property="og:url" content="https://kniost.github.io/2017-03-01-cj0ajvovm001z1js6va278bhl/index.html">
<meta property="og:site_name" content="结网之路">
<meta property="og:description" content="本章主要讲的是 Android 系统的广播机制，并介绍了广播的使用，权限以及有序广播的用法。
GitHub 地址：完成第27章

在使用广播之前，首先回顾一下 PhotoGallery 在本章之前的逻辑：

打开程序后，如果开始推送服务，就每隔一段时间获取一次图片信息
如果图片有更新，就发出通知

那么本章我们想做到的有：

在开机以后自动启用服务（如果打开了开关）
在应用打开时图片有更新也不发">
<meta property="og:updated_time" content="2017-03-14T07:14:30.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android编程权威指南（第二版）学习笔记（二十七）—— 第27章 broadcast intent">
<meta name="twitter:description" content="本章主要讲的是 Android 系统的广播机制，并介绍了广播的使用，权限以及有序广播的用法。
GitHub 地址：完成第27章

在使用广播之前，首先回顾一下 PhotoGallery 在本章之前的逻辑：

打开程序后，如果开始推送服务，就每隔一段时间获取一次图片信息
如果图片有更新，就发出通知

那么本章我们想做到的有：

在开机以后自动启用服务（如果打开了开关）
在应用打开时图片有更新也不发">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"right","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://kniost.github.io/2017-03-01-cj0ajvovm001z1js6va278bhl/"/>





  <title> Android编程权威指南（第二版）学习笔记（二十七）—— 第27章 broadcast intent | 结网之路 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">结网之路</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">临渊羡鱼，不如退而结网</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://kniost.github.io/2017-03-01-cj0ajvovm001z1js6va278bhl/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kniost">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="结网之路">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Android编程权威指南（第二版）学习笔记（二十七）—— 第27章 broadcast intent
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-03-01T15:15:10+08:00">
                2017-03-01
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android编程权威指南学习笔记/" itemprop="url" rel="index">
                    <span itemprop="name">Android编程权威指南学习笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017-03-01-cj0ajvovm001z1js6va278bhl/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017-03-01-cj0ajvovm001z1js6va278bhl/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>本章主要讲的是 Android 系统的广播机制，并介绍了广播的使用，权限以及有序广播的用法。</p>
<p>GitHub 地址：<br><a href="https://github.com/kniost/Android-Programming-Big-Nerd-Ranch-Guide-2nd/commit/762ad934bfe2cd3c50f74cd881875da46eb391d5" target="_blank" rel="external">完成第27章</a></p>
</blockquote>
<p>在使用广播之前，首先回顾一下 PhotoGallery 在本章之前的逻辑：</p>
<ol>
<li>打开程序后，如果开始推送服务，就每隔一段时间获取一次图片信息</li>
<li>如果图片有更新，就发出通知</li>
</ol>
<p>那么本章我们想做到的有：</p>
<ol>
<li>在开机以后自动启用服务（如果打开了开关）</li>
<li>在应用打开时图片有更新也不发出通知</li>
</ol>
<p>这里，我们将使用广播来完成这些任务。</p>
<h1 id="1-接收系统广播：重启后唤醒"><a href="#1-接收系统广播：重启后唤醒" class="headerlink" title="1. 接收系统广播：重启后唤醒"></a>1. 接收系统广播：重启后唤醒</h1><h2 id="1-1-broadcast-intent"><a href="#1-1-broadcast-intent" class="headerlink" title="1.1 broadcast intent"></a>1.1 broadcast intent</h2><p>Android 设备中，各种事件一直在频繁地发生。Wi-Fi 信号时有时无，各种软件包获得安装，电话不时呼入，短信频繁接收等等。许多系统组件需要知道某些事件的发生。为满足这样的需求，Android 提供了 broadcast intent 组件。broadcast intent 的工作原理类似于之前学过的 intent 唯，一不同的是 broadcast intent 可同时被多个叫作 broadcast receiver 的组件接收。</p>
<h2 id="1-2-standalone-receiver"><a href="#1-2-standalone-receiver" class="headerlink" title="1.2 standalone receiver"></a>1.2 standalone receiver</h2><p>standalone receiver 是一个在 manifest 配置文件中声明的 broadcast receiver。即便应用进程已消亡，standalone receiver 也可以被激活。(另一种就是可以同 fragment 或 activity 的生命周期绑定的 dynamic receiver。)</p>
<p>首先建立这样一个 BroadcastReceiver，并重写 onReceive 方法，<strong>注意：该方法是在主线程中执行的</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StartupReceiver</span> <span class="keyword">extends</span> <span class="title">BroadcastReceiver</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">"StartupReceiver"</span>;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onReceive</span><span class="params">(Context context, Intent intent)</span> </span>&#123;</div><div class="line">        Log.i(TAG, <span class="string">"Received broadcast intent: "</span> + intent.getAction());</div><div class="line"></div><div class="line">        <span class="keyword">boolean</span> isOn = QueryPreferences.isAlarmOn(context);</div><div class="line">        PollService.setServiceAlarm(context, isOn);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>记得在 manifest 文件中声明</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">"android.permission.RECEIVE_BOOT_COMPLETED"</span>/&gt;</span></div><div class="line"></div><div class="line"><span class="tag">&lt;<span class="name">application</span>&gt;</span></div><div class="line">	……</div><div class="line">    <span class="tag">&lt;<span class="name">receiver</span> <span class="attr">android:name</span>=<span class="string">".StartupReceiver"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">intent-filter</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">action</span> <span class="attr">android:name</span>=<span class="string">"android.intent.action.BOOT_COMPLETED"</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">receiver</span>&gt;</span></div><div class="line">    ……</div><div class="line"><span class="tag">&lt;/<span class="name">application</span>&gt;</span></div></pre></td></tr></table></figure>
<p>登记好 broadcast receiver 以后，一旦设备启动，这个 receiver 就能接收到启动完成的广播，并随之启动服务了。</p>
<h1 id="2-过滤应用在前台时的通知"><a href="#2-过滤应用在前台时的通知" class="headerlink" title="2. 过滤应用在前台时的通知"></a>2. 过滤应用在前台时的通知</h1><p>为了实现这一点，我们把发出通知的思路改了：之前是在服务中查询到新的结果就发出通知，现在则是：</p>
<ol>
<li>在查询到新的结果后，发出一条应用内的广播并在其中标记一个代码 A，</li>
<li>在应用中动态登记广播接收器，如果接收到广播（说明应用在前台），就把这个代码改成 B。</li>
<li>最后总有一个优先级最低的接收器接收到这个广播，如果代码是 A，就发出通知，否则就不发出通知。</li>
</ol>
<h2 id="2-1-发送-broadcast-intent"><a href="#2-1-发送-broadcast-intent" class="headerlink" title="2.1 发送 broadcast intent"></a>2.1 发送 broadcast intent</h2><p>在 Context 类中直接调用 sendBroadcast(Intent) 即可发出广播。但是为了只让本应用接收到该广播，我们在 manifest 文件中声明一个权限并使用：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">permission</span> <span class="attr">android:name</span>=<span class="string">"com.kniost.photogallery.PRIVATE"</span></span></div><div class="line">                <span class="attr">android:protectionLevel</span>=<span class="string">"signature"</span> /&gt;</div><div class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">"com.kniost.photogallery.PRIVATE"</span> /&gt;</span></div></pre></td></tr></table></figure>
<p>然后使用 sendBroadcast(Intent intent, String permission) 发送通知即可</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sendBroadcast(<span class="keyword">new</span> Intent(ACTION_SHOW_NOTIFICATION), PERM_PRIVATE);</div></pre></td></tr></table></figure>
<h2 id="2-2-动态-broadcast-receiver"><a href="#2-2-动态-broadcast-receiver" class="headerlink" title="2.2 动态 broadcast receiver"></a>2.2 动态 broadcast receiver</h2><p>我们要只在应用开启的时候接受发过来的广播过滤，就不能在 manifest 中声明一个过滤器，而是要动态地建立一个广播接收器。我们在这里建立一个用于隐藏前台通知的通用 fragment 子类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">VisibleFragment</span> <span class="keyword">extends</span> <span class="title">Fragment</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">"VisibleFragment"</span>;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onStart</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>.onStart();</div><div class="line">        IntentFilter filter = <span class="keyword">new</span> IntentFilter(PollService.ACTION_SHOW_NOTIFICATION);</div><div class="line">        getActivity().registerReceiver(mOnShowNotification, filter,</div><div class="line">                PollService.PERM_PRIVATE, <span class="keyword">null</span>);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onStop</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>.onStop();</div><div class="line">        getActivity().unregisterReceiver(mOnShowNotification);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> BroadcastReceiver mOnShowNotification = <span class="keyword">new</span> BroadcastReceiver() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onReceive</span><span class="params">(Context context, Intent intent)</span> </span>&#123;</div><div class="line">            <span class="comment">// 如果接收到广播，说明应用正在前台，所以把 ResultCode 更改掉</span></div><div class="line">            Log.i(TAG, <span class="string">"canceling notification"</span>);</div><div class="line">            setResultCode(Activity.RESULT_CANCELED);</div><div class="line">        &#125;</div><div class="line">    &#125;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<blockquote>
<p>为什么在 start 和 stop 中登记和撤销 receiver 呢？因为在 retain fragment 中 onCreate(…)和 onDestroy()方法中的 getActivity()方法在设备旋转时会返回不同的值。因此如果想在 Fragment.onCreate(Bundle)和 Fragment.onDestroy()方法中实现登记或撤销登记，应使用 getActivity().getApplicationContext()方法。</p>
</blockquote>
<h2 id="2-3-使用有序-broadcast"><a href="#2-3-使用有序-broadcast" class="headerlink" title="2.3 使用有序 broadcast"></a>2.3 使用有序 broadcast</h2><p>如果想让程序在打开时不发送出通知，就不能再让服务来发出通知了，因为它无法知道前台的运行状态。所以我们让 PollService 发送一个有序广播。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">Notification notification = ……;</div><div class="line"></div><div class="line">Intent i = <span class="keyword">new</span> Intent(ACTION_SHOW_NOTIFICATION);</div><div class="line">i.putExtra(REQUEST_CODE, <span class="number">0</span>);</div><div class="line">i.putExtra(NOTIFICATION, notification);</div><div class="line">sendOrderedBroadcast(i, PERM_PRIVATE, <span class="keyword">null</span>, <span class="keyword">null</span>,</div><div class="line">        Activity.RESULT_OK, <span class="keyword">null</span>, <span class="keyword">null</span>);</div></pre></td></tr></table></figure>
<p>有序广播是按照优先级发送的，先发送给优先级高的接收器，再发给优先级低的接收器。因为在应用结束后也要发出通知，显然我们发出通知的广播接收器是需要声明在 manifest 文件中的。</p>
<p>内部实现如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">NotificationReceiver</span> <span class="keyword">extends</span> <span class="title">BroadcastReceiver</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">"NotificaitonReceiver"</span>;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onReceive</span><span class="params">(Context context, Intent intent)</span> </span>&#123;</div><div class="line">        Log.i(TAG, <span class="string">"received result: "</span> + getResultCode());</div><div class="line">        <span class="keyword">if</span> (getResultCode() != Activity.RESULT_OK) &#123;</div><div class="line">            <span class="comment">// PollService 发出的 intent 带的结果码是 RESULT_OK</span></div><div class="line">            <span class="comment">// 如果接到的不是，说明应用在前台，将结果码修改了</span></div><div class="line">            <span class="keyword">return</span>;</div><div class="line">        &#125;</div><div class="line"></div><div class="line">			<span class="comment">// 如果没有 return，说明应用不在前台，就可以发出通知了。</span></div><div class="line">        <span class="keyword">int</span> requestCode = intent.getIntExtra(PollService.REQUEST_CODE, <span class="number">0</span>);</div><div class="line">        Notification notification = (Notification)</div><div class="line">                intent.getParcelableExtra(PollService.NOTIFICATION);</div><div class="line"></div><div class="line">        NotificationManagerCompat notificationManager =</div><div class="line">                NotificationManagerCompat.from(context);</div><div class="line">        notificationManager.notify(requestCode, notification);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">receiver</span> <span class="attr">android:name</span>=<span class="string">".NotificationReceiver"</span></span></div><div class="line">          <span class="attr">android:exported</span>=<span class="string">"false"</span>&gt;</div><div class="line">    <span class="comment">&lt;!-- 在这里将优先级设为最低，即 -999 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">intent-filter</span></span></div><div class="line">        <span class="attr">android:priority</span>=<span class="string">"-999"</span>&gt;</div><div class="line">        <span class="tag">&lt;<span class="name">action</span> <span class="attr">android:name</span>=<span class="string">"com.kniost.photogallery.SHOW_NOTIFICATION"</span> /&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">receiver</span>&gt;</span></div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Android/" rel="tag"># Android</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017-02-23-cj0ajvovo00221js69nx2lou8/" rel="next" title="Android编程权威指南（第二版）学习笔记（二十六）—— 第26章 后台服务">
                <i class="fa fa-chevron-left"></i> Android编程权威指南（第二版）学习笔记（二十六）—— 第26章 后台服务
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017-03-02-cj0ajvovl001w1js6jq55crvh/" rel="prev" title="Android编程权威指南（第二版）学习笔记（二十八）—— 第28章 网页浏览">
                Android编程权威指南（第二版）学习笔记（二十八）—— 第28章 网页浏览 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="disqus_thread"></div>
<script>

/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
/*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://kniost-comment-github.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
                                
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.jpg"
               alt="kniost" />
          <p class="site-author-name" itemprop="name">kniost</p>
           
              <p class="site-description motion-element" itemprop="description">kniost 的成长与疑惑</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">31</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-接收系统广播：重启后唤醒"><span class="nav-text">1. 接收系统广播：重启后唤醒</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-broadcast-intent"><span class="nav-text">1.1 broadcast intent</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-standalone-receiver"><span class="nav-text">1.2 standalone receiver</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-过滤应用在前台时的通知"><span class="nav-text">2. 过滤应用在前台时的通知</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-发送-broadcast-intent"><span class="nav-text">2.1 发送 broadcast intent</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-动态-broadcast-receiver"><span class="nav-text">2.2 动态 broadcast receiver</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-使用有序-broadcast"><span class="nav-text">2.3 使用有序 broadcast</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kniost</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  

    <script type="text/javascript">
      var disqus_shortname = 'kniost-comment-github';
      var disqus_identifier = '2017-03-01-cj0ajvovm001z1js6va278bhl/';

      var disqus_title = "Android编程权威指南（第二版）学习笔记（二十七）—— 第27章 broadcast intent";


      function run_disqus_script(disqus_script) {
        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
      }

      run_disqus_script('count.js');

      
        var disqus_config = function () {
            this.page.url = disqus_url;
            this.page.identifier = disqus_identifier;
            this.page.title = disqus_title;
        };
        run_disqus_script('embed.js');
      

    </script>
  










  
  

  

  

  

  


  

</body>
</html>
